﻿Imports System.Windows.Forms
Imports TyranntEngine

Public Class RegisterControl
    Private WithEvents _client As New TyranntOnlineUserService.TyranntUserServiceClient
    Private _token As New TyranntOnlineUserService.TokenType

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        EnableForm(False)
        usernameTextBox.BackColor = Color.White
        emailTextBox.BackColor = Color.White
        Dim aRegister As New TyranntOnlineUserService.RegisterType
        aRegister.Username = usernameTextBox.Text
        aRegister.UserAlias = aliasTextBox.Text
        aRegister.Password = EncryptPassword(passwordTextBox.Text)
        aRegister.Email = emailTextBox.Text
        passwordTextBox.Text = ""
        RepeatPasswordTextBox.Text = ""
        _client.RegisterAsync(aRegister)
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub usernameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles usernameTextBox.TextChanged
        CheckForm()
    End Sub

    Private Sub aliasTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles aliasTextBox.TextChanged
        CheckForm()
    End Sub

    Private Sub passwordTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles passwordTextBox.TextChanged
        CheckForm()
    End Sub

    Private Sub RepeatPasswordTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RepeatPasswordTextBox.TextChanged
        CheckForm()
    End Sub

    Private Sub emailTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles emailTextBox.TextChanged
        CheckForm()
    End Sub

    Private Sub CheckForm()
        Dim filledIn As Boolean = True
        If usernameTextBox.Text = "" Or passwordTextBox.Text = "" Or RepeatPasswordTextBox.Text = "" Or aliasTextBox.Text = "" Or emailTextBox.Text = "" Then
            filledIn = False
        End If
        If passwordTextBox.Text <> "" And RepeatPasswordTextBox.Text <> "" Then
            If passwordTextBox.Text <> RepeatPasswordTextBox.Text Then
                passwordTextBox.BackColor = Color.LightSalmon
                RepeatPasswordTextBox.BackColor = Color.LightSalmon
                filledIn = False
            Else
                passwordTextBox.BackColor = Color.LightGreen
                RepeatPasswordTextBox.BackColor = Color.LightGreen
            End If
        Else
            passwordTextBox.BackColor = Color.White
            RepeatPasswordTextBox.BackColor = Color.White
        End If
        If emailTextBox.Text.Contains("@") = False Or emailTextBox.Text.Contains(".") = False Then
            filledIn = False
        End If
        If filledIn = True Then
            OK_Button.Enabled = True
        Else
            OK_Button.Enabled = False
        End If
    End Sub

    Private Sub RegisterControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CheckForm()
    End Sub

    Private Sub _client_RegisterCompleted(ByVal sender As Object, ByVal e As TyranntOnlineUserService.RegisterCompletedEventArgs) Handles _client.RegisterCompleted
        _token = e.Result

        If _token.TokenString.StartsWith("[#]") Then
            If _token.TokenString.StartsWith("[#]failed[") Then
                MessageBox.Show(_token.TokenString, "Fatal Logon Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf _token.TokenString.StartsWith("[#]userexists") Then
                Me.Text = "Username already taken"
                emailTextBox.BackColor = Color.White
                usernameTextBox.BackColor = Color.LightYellow
            ElseIf _token.TokenString.StartsWith("[#]emailexists") Then
                Me.Text = "Email address already exists"
                emailTextBox.BackColor = Color.LightYellow
                usernameTextBox.BackColor = Color.White
            Else
                MessageBox.Show(_token.TokenString, "Unknown Error Code", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
            EnableForm(True)
        Else
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub

    Private Sub EnableForm(ByVal enabled As Boolean)
        usernameTextBox.Enabled = enabled
        aliasTextBox.Enabled = enabled
        passwordTextBox.Enabled = enabled
        RepeatPasswordTextBox.Enabled = enabled
        emailTextBox.Enabled = enabled
        If enabled = True Then
            CheckForm()
        End If
    End Sub

    Public ReadOnly Property UserAlias() As String
        Get
            Return _token.UserAlias
        End Get
    End Property

    Public ReadOnly Property UserGroup() As Integer
        Get
            Return _token.UserGroup.IntegerValue
        End Get
    End Property

    Public ReadOnly Property TokenString() As String
        Get
            Return _token.TokenString
        End Get
    End Property

End Class
